<?php

namespace Admin\Controller;

use Think\Controller;

/**
 * 模型管理
 */
class SinglePageController extends CommonController {

    public function index() {
        $list = $this->lists("SinglePage", ['status' => ['egt', 0]]);
        $this->assign("_list", $list);
        Cookie('__forward__', $_SERVER['REQUEST_URI']);
        $this->meta_title = '单页面管理';
        $this->display();
    }

    /**
     * 添加
     */
    public function add() {

        if (IS_POST) {
            $singlePage = D("SinglePage");
            if ($singlePage->create()) {
                if ($singlePage->add()) {
                    $this->success("操作成功",Cookie('__forward__'));
                } else {
                    $this->error("操作失败");
                }
            } else {
                $this->error($singlePage->getError());
            }
        }
        $index_path = C("index_tmpl_path");
        $index_path = $index_path . CONTROLLER_NAME . "/*";
        /** 获取模板文件 * */
        $theme_path_tmpl = array_map("basename", glob($index_path));

        $this->assign("theme_path_tmpl", $theme_path_tmpl);
        $this->meta_title = '单页面添加';

        $this->display();
    }
      /**
     * 改变数据库操作
     */
    public function changestatus() {
        $method = I("method");
        $id = I("id", 0);
        $status = I("status");
        if ($status == 1) {
            $status = 0;
        } else {
            $status = 1;
        }
        switch ($method) {
            case "cgStatus": //审核状态
                $data["status"] = $status;
                return $this->changeFieldData("SinglePage", array("id" => $id), $data);
                break;
            case "cgIstop" :  //是否置顶
                $data["istop"] = $status;

                return $this->changeFieldData("SinglePage", array("id" => $id), $data);
                break;
            case "cgRecommended" :  //是否推荐
                $data["recommended"] = $status;
                return $this->changeFieldData("SinglePage", array("id" => $id), $data);
                break;

            case "deleteArticle" :  //删除文章
                return $this->delete("SinglePage", array("id" => $id));
                break;
            case "recoveryPage":   //恢复单页面
                $data["status"] = $status;
                return $this->changeFieldData("SinglePage", array("id" => $id), $data);
                break;
        }
    }

    /**
     * 编辑
     */
    public function edit() {
        $id = I("id");
        if (IS_POST) {
            $singlePage = D("SinglePage");
            if ($singlePage->create()) {
                if ($singlePage->save()) {
                    $this->success("操作成功",Cookie('__forward__'));
                } else {
                    $this->error("操作失败");
                }
            } else {
                $this->error($singlePage->getError());
            }
        }

        $info = D("SinglePage")->where(array("id" => $id))->find();
        $this->assign("info", $info);
        $index_path = C("index_tmpl_path");
        $index_path = $index_path . CONTROLLER_NAME . "/*";
        /** 获取模板文件 * */
        $theme_path_tmpl = array_map("basename", glob($index_path));
        $this->assign("theme_path_tmpl", $theme_path_tmpl);
        $this->meta_title = '单页面编辑';
        $this->display();
    }
/**
     *  回收站
     */
    public function trash() {
          
         $list = $this->lists("SinglePage", ['status' => -1]);
         $this->assign("_list",$list);
        $this->meta_title = '单页面列表';
        Cookie('__forward__', $_SERVER['REQUEST_URI']);
        return $this->display();
    }
    
      /**
     * 数据强制删除
     */
    public function del($id) {
        
        if (D("SinglePage")->where(array("id"=>$id))->delete()) {
            return $this->success("删除成功");
        } else {
            return $this->error("删除失败");
        }
    }
}
